#include "Device_cJSON.h"
#include "json_protocol.h"
#include "DHT11.h"
#include "stm32f10x.h"
#include "usart3.h"
#include "USART_Model.h"
#include "RTC_Timestamp.h"
#include "Motor.h"
#include "led.h"
#include "cJSON.h"  // 添加这行

// 简化光照数据发送
void send_light_data(void) {
    uint8_t light_value = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12);
    
    char* json_data = create_light_data(light_value);
    if (json_data != NULL) {
        Send_String(json_data);
        USART3_SendString(json_data); // 调试串口
        free_json_string(&json_data);
    }
}

// 简化温湿度数据发送
void send_dht11_data(void) {
    DHT11 dht11 = { .humi_int = 0, .humi_deci = 0, .temp_int = 0, .temp_deci = 0, .check = 0 };
    getDHT11Data(&dht11);
    
    // 合并整数和小数部分
    float humidity = dht11.humi_int + dht11.humi_deci / 10.0f;
    float temperature = dht11.temp_int + dht11.temp_deci / 10.0f;
    
    char* json_data = create_dht11_data(humidity, temperature);
    if (json_data != NULL) {
        // 确保一次性发送完整JSON
        Send_String(json_data);
        USART3_SendString(json_data);
        free_json_string(&json_data);
        
        // 添加小延时，避免数据冲突
        Delay_ms(10);
    }
}

// 传感器组数据发送（包含光照和CO2）
void send_sensor_group_data(uint32_t CO2_PPM) {
    uint8_t light_value = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12);
    
    // 创建完整的JSON字符串，确保不截断
    char json_buffer[128];
    if (CO2_PPM > 10000) CO2_PPM = 0; // 过滤异常CO2值
    
    snprintf(json_buffer, sizeof(json_buffer), 
             "{\"type\":\"sensor_group\",\"light\":%d,\"co2\":%lu,\"time\":%lu}",
             light_value, CO2_PPM, get_timestamp());
    
    Send_String(json_buffer);
    USART3_SendString(json_buffer);
}

// 简化命令处理
void process_json_command(const char* json_str) {
    CommandType cmd_type;
    int result = parse_command(json_str, &cmd_type);
    
    if (result != JSON_SUCCESS) {
        char* error_response = create_command_response("error", "命令解析失败");
        if (error_response != NULL) {
            Send_String(error_response);
            free_json_string(&error_response);
        }
        return;
    }
    
    switch (cmd_type) {
        case CMD_SET_TIME: {
            time_t timestamp;
            if (parse_time_command(json_str, &timestamp) == JSON_SUCCESS) {
                RTC_SetDateTime(timestamp);
                char* response = create_command_response("success", "时间设置成功");
                if (response != NULL) {
                    Send_String(response);
                    free_json_string(&response);
                }
            }
            break;
        }
        
        case CMD_FAN_CONTROL: {
            FanControl fan;
            if (parse_fan_command(json_str, &fan) == JSON_SUCCESS) {
                Motor_SetSpeed(fan.power, fan.speed);
                char* response = create_command_response("success", "风扇控制成功");
                if (response != NULL) {
                    Send_String(response);
                    free_json_string(&response);
                }
            }
            break;
        }
        
        case CMD_LED_CONTROL: {
            LedControl led;
            if (parse_led_command(json_str, &led) == JSON_SUCCESS) {
                // 改为使用有效的ID，比如1
                LED_control(1, led.power, led.brightness);
                char* response = create_command_response("success", "LED控制成功");
                if (response != NULL) {
                    Send_String(response);
                    free_json_string(&response);
                }
            }
            break;
        }
        
        case CMD_SENSOR_DATA: {
            // 只发送温湿度数据，不发送其他传感器数据
            send_dht11_data();
            break;
        }
        
        default: {
            char* response = create_command_response("error", "未知命令类型");
            if (response != NULL) {
                Send_String(response);
                free_json_string(&response);
            }
            break;
        }
    }
}